<?php
namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxAssignment;
use Illuminate\Support\Str;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Support\JavaScript;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxAssignmentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxAssignment(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.username', '姓名')->display(function ($username) {
                return $username;
            });
            $grid->column('title');
            $grid->column('content');
            $grid->column('integral');
            $grid->column('images')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $grid->column('status')->using(['0' => '未打卡', '1' => '打卡成功', '2' => '未审核'])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default    
                1 => 'success',
                2 => 'danger',
                0 => 'primary',
            ]);
            $grid->column('date');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxAssignment(), function (Show $show) {
            $show->field('id');
            // $show->field('user_id');
            $show->field('title');
            $show->field('content');
            $show->field('integral');
            // $show->field('images');
            $show->column('images')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $show->field('status');
            $show->field('date');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxAssignment(), function (Form $form) {
            $form->display('id');
            // $form->text('user.username');
            $form->select('user_id', '选择学员')->required()->options([
                1 => '王浩浩',
                2 => '刘小浩',
                3 => '王小浩',
                4 => '王暄炀',
                5 => '任小粟',
            ]);
                 
            $form->text('title')->required();
            // $form->text('');
            $form->markdown('content','商品详情')->required()->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public')->required()->disable();
            $form->number('integral')->required();
            // $form->image('images')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(512)->autoUpload()->help('大小不要超过512K');
            // $form->radio('status')->options(['0' => '未打卡', '1' => '打卡成功', '2' => '打卡失败'])->required()->default('0')->disable();
            $form->date('date')->format('YYYY-MM-DD')->default(date('Y-m-d'))->required();
        });
    }
}
